import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:flutter_syt/api/api.dart';
import 'package:flutter_syt/application.dart';
import 'package:flutter_syt/datas/banner_item.dart';
import 'package:flutter_syt/datas/weather_item.dart';
import 'package:flutter_syt/domain/home/home_cover.dart';
import 'package:flutter_syt/domain/news/news_cover.dart';
import 'package:flutter_syt/domain/widgets/stupid_title_bar.dart';

StupidTitleBarEntity? weatherEntity;
List<HomeCoverEntity> homeBannerList = [];
List<NewsCoverEntity> newsBannerList = [];

queryWeather() async {
  try {
    // 天气
    WeatherItem weather = await API.shared.common.weather();
    debugPrint("${weather.json}");
    var local =
        appContext.storage.getString("storyLocal") ?? Intl.getCurrentLocale();
    weatherEntity = StupidTitleBarEntity.create(
      weather.icon,
      '${weather.temp}',
      local.contains('zh') ? "成都" : "Chengdu",
    );
  } catch (e, stack) {
    debugPrintStack(stackTrace: stack);
  }
}

fetchHomeBanner() async {
  // banner
  try {
    List<BannerItem> items = await API.shared.common.banner();
    homeBannerList.clear();
    homeBannerList.addAll(items
        .map(
          (e) => HomeCoverEntity.create(
            e.path,
            e.time,
            e.title,
            e.advert_pic_short,
            e.color,
          ),
        )
        .toList());
  } catch (e, stack) {

    debugPrintStack(stackTrace: stack);
  }
}

fetchNewsBanner() async {
  // banner
  try {
    List<BannerItem> items = await API.shared.common.banner();
    newsBannerList.clear();
    newsBannerList.addAll(items
        .map(
          (e) => NewsCoverEntity.create(
              e.isHot, e.title, e.path, e.advert_pic, e.color),
        )
        .toList());
  } catch (e, stack) {
    debugPrintStack(stackTrace: stack);
  }
}
